package com.yh.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yh.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
public class AjaxController {
    //json字符串：{"username":"tom","password":"111"}
    @RequestMapping("ajaxreq")
    public String ajaxreq(@RequestBody String json){
        System.out.println(json);
        return "success";
    }
    //user对象:这个使用更多一点;
    @RequestMapping("ajaxreq2")
    public String ajaxreq2(@RequestBody User user){
        System.out.println(user);
        return "success";
    }


    @RequestMapping("ajaxreq3")
    public String ajaxreq3(@RequestBody String json) throws IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        User user = objectMapper.readValue(json, User.class);
        System.out.println(user);
        return "success";
    }
    //------------------异步响应------------

    /**
     * 当我们没有指定String类型，这个时候意味着，么有对应页面;
     *   返回的ajaxresp.jsp,ajaxresp对应的jsp;url.jsp
     * @return
     */
    @RequestMapping("ajaxresp")
    @ResponseBody
    public User ajaxresp22(){
        User user=new User();
        user.setUsername("tom.ma");
        user.setPassword("666888");
        System.out.println(user);
        return user;
    }

}

